﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class DBHelper
    {
        private static string constr = "server=LAPTOP-MCU8SB1M;database=school;uid=sa;pwd=123456789";
        private static string sql = "select * from userinfo";

        public static void printUserInfoReader()
        {

            string constr = "server=LAPTOP-MCU8SB1M;database=school;uid=sa;pwd=123456789";
            SqlConnection conn = new SqlConnection(constr);
            string sql = "select * from userinfo";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            var sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                Console.WriteLine("{0}\t{1}\t{2}\t{3}", sdr["userid"], sdr["username"], sdr["userpwd"], sdr["userage"], sdr["usersex"]);
            }
            conn.Close();


        }


        public static void PrintUserInfoAdaper()
        {


            SqlDataAdapter sqldataAdapter = new SqlDataAdapter(sql, constr);


            DataTable dataTable = new DataTable();

            sqldataAdapter.Fill(dataTable);

            Console.WriteLine();

            Console.WriteLine();

            Console.WriteLine();

            Console.WriteLine();

            Console.WriteLine();

            Console.WriteLine("{0}\t{1}\t{2}\t{3}", "id", "姓名", "密码", "年龄", "性别");

            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                Console.WriteLine("{0}\t{1}\t{2}\t{3}", dataTable.Rows[i]["userid"], dataTable.Rows[i]["username"], dataTable.Rows[i]["userpwd"], dataTable.Rows[i]["userage"], dataTable.Rows[i]["usersex"]);

            }

        }

    }
}
